package ssm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ssm.dao.LocationCard;
import ssm.dao.TrackCard;
import ssm.dao.YktBusiness;
import ssm.model.LocationCardMapper;
import ssm.model.TrackCardMapper;
import ssm.model.TrackMapper;

import javax.management.ObjectName;
import java.util.*;

@Service
public class TrackCardServiceImpl implements TrackCardService {

    @Autowired
    private TrackCardMapper trackCardMapper;

    @Autowired
    private TrackMapper trackMapper;

    @Autowired
    private LocationCardMapper locationCardMapper;

    public Map<String, Object> getByTimeOrJid_business(List<String> list, String jid, String starttime, String endtime, Integer page, Integer pageNum,Integer lx) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!list.isEmpty() && list != null) {
            map.put("list", list);
        }
        if (!list.isEmpty() && list != null) {
            map.put("lx", lx);
        }
        if (jid != null && jid != "") {
            map.put("jid", jid);
        }
        if (starttime != null && starttime != "") {
            map.put("starttime", starttime);
        }

        if (endtime != null && endtime != "") {
            map.put("endtime", endtime);
        }
        if (page != -1 && pageNum != 0) {
           int pageStart=(page)*pageNum;
            map.put("pageStart", pageStart);
            map.put("pageEnd", pageNum);
        }

        System.out.println("map:" + map);
        List<YktBusiness> yktBusinesses = trackMapper.getBussines_limt(map);

        List<TrackCard> card = ToTrackCard(yktBusinesses);
        //List<TrackCard>cards=ToTrack
        Long total = trackMapper.getBussines_total(map);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("data", card);
        result.put("total", total);

        return result;
    }

    private List<TrackCard> ToTrackCard(List<YktBusiness> yktBusinesses) {
        List<TrackCard> cards = new ArrayList<TrackCard>();
        for (YktBusiness bs : yktBusinesses) {
            TrackCard card = new TrackCard();
            card.setId(bs.getId());
            card.setXm(trackMapper.getxm(bs.getKh()));
            card.setSksj(bs.getJysj());
            card.setSkdd(bs.getShmc());
            card.setSklsh(bs.getLsh());
            card.setSklx(bs.getShlxmc());
            card.setRylx(trackMapper.getRylx(bs.getKh()));
            card.setXhgh(bs.getKh());
            card.setXfje(bs.getJye());
            card.setSbbh(null);
            card.setLy("ykt_business");
            LocationCard locationCard = locationCardMapper.getByMc(bs.getShmc());
           // System.out.println("local查询:" + new Date());
            if (locationCard != null) {
                card.setCamerjd(locationCard.getDevicejd());
                card.setCamerwd(locationCard.getDevicewd());
            }
            cards.add(card);
        }
        return cards;
    }

    public List<TrackCard> getByTimeOrJid_track(List<String> list, String jid, String starttime, String endtime, int page, int pageNum) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (jid != null || !jid.equals("")) {
            map.put("jid", jid);
        }
        if (!list.isEmpty()) {
            map.put("list", list);
        }
        if (page != 0 && pageNum != 0) {
            int pageStart=(page-1)*pageNum;
            int pageEnd=(page)*pageNum-1;
            map.put("pageStart", pageStart);
            map.put("pageEnd", pageEnd);
        }
        map.put("starttime", starttime);
        map.put("endtime", endtime);
        List<TrackCard> cards = trackCardMapper.getByTimeOrJid(map);
        return cards;
    }
}
